package com.ruoyi.web.controller.project;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IAchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 需求Controller
 *
 * @author EdwardXRX
 * @date 2024-04-12
 */
@RestController
@RequestMapping("/project/achievement")
public class AchievementController extends BaseController {
    @Autowired
    private IAchievementService achievementService;


    /**
     * 查询需求列表
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        startPage();
        project.setType(3);
        Long userId = getUserId();
        List<Project> list = new ArrayList<>();
        if (getLoginUser().getUserId() == 1) {
            list = achievementService.selectProjectList(project);
        } else {
            list = achievementService.selectProjectListByUserId(userId);
        }
        return getDataTable(list);
    }

    @GetMapping("/listAchievementByClickNum")
    public TableDataInfo listAchievementByClickNum(Project project) {
        startPage();
        project.setType(3);
        List<Project> list = new ArrayList<>();
        list = achievementService.listAchievementByClickNum();
        return getDataTable(list);
    }


    /**
     * 导出需求列表
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:export')")
    @Log(title = "需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project) {
        List<Project> list = achievementService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "需求数据");
    }

    /**
     * 获取需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(achievementService.selectProjectById(id));
    }

    /**
     * 新增需求
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:add')")
    @Log(title = "需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project achievement) {
        Long userId = getUserId();
        achievement.setType(3);
        achievement.setChecker1(1000043642L);
        achievement.setChecker2(1000068884L);
        achievement.setClickNum(0);
        achievement.setUserId(userId);
        achievement.setCreateTime(new Date());
        //设置为不可见
        achievement.setIsShow(0);
        //设置待审核的需求
        achievement.setState(0);
        return toAjax(achievementService.insertProject(achievement));
    }

    /**
     * 修改需求
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:edit')")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {
        return toAjax(achievementService.updateProject(project));
    }

    /**
     * 删除需求
     */
    @PreAuthorize("@ss.hasPermi('project:achievement:remove')")
    @Log(title = "需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(achievementService.deleteProjectByIds(ids));
    }
}
